<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .box {
            width: 60%;
            margin: 20px auto;
        }

        table {
            width: 100%;
            background: black;
        }

        tr {
            background: white;
            text-align: center;
        }

        button {
            margin: 0 3px;
        }

        .muBu {
            width: 100%;
            height: 100%;
            position: fixed;
            background: rgba(0, 0, 0, 0.3);
            display: none;
        }

        .kuang {
            width: 300px;
            height: 150px;
            line-height: 130px;
            border-radius: 6px;
            background: rgba(255, 255, 255, 0.9);
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
            text-align: center;
        }

        .cuo {
            width: 26px;
            border-radius: 50%;
            height: 26px;
            line-height: 24px;
            color: red;
            font-size: 26px;
            position: absolute;
            top: 0;
            right: 0;
            background: #999;
            cursor: pointer;
        }

        .queDing,
        .quXiao {
            width: 80px;
            height: 40px;
            margin: 0 20px;
        }
    </style>
</head>

<body>
    <div class="muBu">
        <div class="kuang">
            <span class="cuo">×</span>
            <button class="queDing">确定</button>
            <button class="quXiao">取消</button>
        </div>
    </div>
    <div class="box">
        <table border="0" cellspacing="1">
            <thead>
                <tr>
                    <th>序号</th>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>性别</th>
                    <th>爱好</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <!-- <tr>
                    <td>${i+1}</td>
                    <td>${arr[i].userName}</td>
                    <td>${arr[i].age}</td>
                    <td>${arr[i].sex}</td>
                    <td>${arr[i].hobby}</td>
                    <td><button onclick="shang(${arr[i].id})">上移</button><button onclick="shang(${arr[i].id})">删除</button><button onclick="shang(${arr[i].id})">下移</button></td>
                </tr> -->
            </tbody>
        </table>
    </div>
    <script>
        var tbody = document.querySelector('tbody');
        var shangs = document.getElementsByClassName('shang');
        var xias = document.getElementsByClassName('xia');
        var muBu = document.querySelector('.muBu');
        var cuo = document.querySelector('.cuo');
        var queDing = document.querySelector('.queDing');
        var quXiao = document.querySelector('.quXiao');
        var arr;
        window.onload = function () {
            if (localStorage.obj) {
                arr = JSON.parse(localStorage.obj).arr;
                diaoYong(arr);
            } else {
                arr = [];
            }
        }
        function diaoYong(arr) {
            var str = '';
            for (var i = 0; i < arr.length; i++) {
                str += `<tr>
                    <td>${i + 1}</td>
                    <td>${arr[i].userName}</td>
                    <td>${arr[i].age}</td>
                    <td>${arr[i].sex}</td>
                    <td>${arr[i].hobby}</td>
                    <td><button class="shang" onclick="shang(${arr[i].id})">上移</button><button onclick="del(${arr[i].id})">删除</button><button class="xia" onclick="xia(${arr[i].id})">下移</button></td>
                </tr>`
            }
            tbody.innerHTML = str;
            shangs[0].disabled = true;
            xias[xias.length - 1].disabled = true;
        }


        function shang(id) {
            var obj1;
            for (var i = 0; i < arr.length; i++) {
                if (id == arr[i].id && i != 0) {
                    obj1 = arr[i];
                    arr[i] = arr[i - 1];
                    arr[i - 1] = obj1;
                }
            }
            var obj = {
                arr: arr
            }
            localStorage.obj = JSON.stringify(obj);
            diaoYong(arr);
        }
        function xia(id) {
            var obj2;
            for (var i = 0; i < arr.length; i++) {
                if (id == arr[i].id && i != arr.length - 1) {
                    obj2 = arr[i + 1];
                    arr[i + 1] = arr[i];
                    arr[i] = obj2;
                    break;
                }
            }
            var obj = {
                arr: arr
            }
            localStorage.obj = JSON.stringify(obj);
            diaoYong(arr);
        }
        function del(id) {
            muBu.style.display = 'block';
            cuo.id = id;
            queDing.id = id;
            quXiao.id = id;
            queDing.onclick = function () {
                for (var i = 0; i < arr.length; i++) {
                    if (queDing.id == arr[i].id) {
                        arr.splice(i, 1);
                    }
                }
                muBu.style.display = 'none';
                var obj = {
                arr: arr
            }
            localStorage.obj = JSON.stringify(obj);
            diaoYong(arr);
            }
        }
        cuo.onclick = function () {
            muBu.style.display = 'none';
        }
        quXiao.onclick = function () {
            muBu.style.display = 'none';
        }
    </script>
    <script src="js/shangxia.js"></script>
</body>

</html>